package com.website.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.website.common.controller.BaseController;
import com.website.common.warpper.ResultMap;
import com.website.system.model.FlowerType;
import com.website.system.model.Flowers;
import com.website.system.service.FlowerTypeService;
import com.website.system.service.FlowersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("manage/flowerType")
public class FlowerTypeController extends BaseController {

    @Resource
    private FlowerTypeService flowerTypeService;

    @Resource
    private FlowersService flowersService;

    /**
     * 跳转到内容页面
     * @return
     */
    @RequestMapping("")
    public String flowerType(){
        return "manage/flowerType";
    }

    /**
     * 分页查询花卉类型所有信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/flowerTypeTable")
    public ResultMap flowerTypeTable(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam(required = false) String fname){
        ResultMap<List<FlowerType>> map = new ResultMap<>(SUCCESS_CODE, SUCCESS_MSG, null, null);
        Page<FlowerType> pageInfo = new Page<>(page,limit);
        Wrapper<FlowerType> wrapper = new EntityWrapper<>();
        wrapper = fname == null ? wrapper : wrapper.like("fname", fname);
        List<FlowerType> flowerType = flowerTypeService.selectPage(pageInfo,wrapper).getRecords();
        map.setCount(flowerTypeService.selectCount(wrapper));
        map.setData(flowerType);
        return map;
    }

    //查看
    @RequestMapping("/readFlowerType")
    public String readFType(Model model, @RequestParam Integer fid) {
        model.addAttribute("flowerType", flowerTypeService.selectById(fid));
        return "manage/readFlowerType";
    }

    /**
     * 删除判断
     **/
    @ResponseBody
    @RequestMapping(value="/isDelFType", method = RequestMethod.POST)
    public Object isDeleteComputer(Integer id){
        //Wrapper<FlowerType> fTypeWrapper=new EntityWrapper<>();
        Wrapper<Flowers> flowersWrapper = new EntityWrapper<>();
        //fTypeWrapper =id ==null?fTypeWrapper:fTypeWrapper.eq("fid", id);
        flowersWrapper = id == null?flowersWrapper:flowersWrapper.eq("fid",id);
        int i=flowersService.selectCount(flowersWrapper);
        //根据类型的fid查询出商品中都是这个fid的商品 所以用商品的selCount进行查询
        //System.out.println("-------------------"+i);
        if(i>0){
            return true;
        }else{
            return false;
        }
    }

    //刪除
    @ResponseBody
    @RequestMapping(value = "/delFlowerType", method = RequestMethod.POST)
    public String delFType(@RequestParam Integer fid) {
        try {
            flowerTypeService.deleteById(fid);
            return SUCCESS_MSG;
        } catch (Exception e) {
            return ERROR_MSG;
        }
    }

    //跳转到修改页面
    @RequestMapping("/toUpdateFlowerType")
    public String updateFType(Model model, @RequestParam Integer fid) {
        model.addAttribute("flowerType", flowerTypeService.selectById(fid));
        return "manage/updateFlowerType";
    }

    //修改
    @ResponseBody
    @RequestMapping("/updateFlowerTypes")
    public String updateFType(FlowerType flowerType){
        try {
            /*FlowerType fType=flowerTypeService.selectById(flowerType.getFid());
            fType.setFname(flowerType.getFname());
            fType.setDetail(flowerType.getDetail());*/
            flowerTypeService.updateById(flowerType);
            return SUCCESS_MSG;
        } catch (Exception e) {
            return ERROR_MSG;
        }
    }

    //跳转到添加页面
    @RequestMapping("/toAddFlowerType")
    public String toAddFType(){
        return "manage/addFlowerType";
    }

    //添加
    @ResponseBody
    @RequestMapping("/addFlowerTypes")
    public String addFType(FlowerType flowerType){
        flowerTypeService.insert(flowerType);
        return "添加成功";
    }

    /**
     * 查看花卉类型
     */
    @RequestMapping("/selectFType")
    @ResponseBody
    public List<FlowerType> selectFType() {
        List<FlowerType> fTypes = flowerTypeService.selectList(new EntityWrapper<>());
        return fTypes;
    }

}
